﻿namespace SharpGym.Silverlight.Views.Aluno
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using SharpService;

    public partial class EditAluno : UserControl
    {
        // Using a DependencyProperty as the backing store for Busy.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BusyProperty =
            DependencyProperty.Register("Busy", typeof (BusyIndicator), typeof (EditAluno), null);

        public EditAluno()
        {
            InitializeComponent();
        }

        public BusyIndicator Busy
        {
            get { return (BusyIndicator) GetValue(BusyProperty); }
            set { SetValue(BusyProperty, value); }
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            Busy.IsBusy = true;

            DateTime dateTime;

            DateTime.TryParse(Aniversario.Text, out dateTime);

            var aluno = (AlunoDto) DataContext;

            aluno.Nome = Nome.Text;
            aluno.DataNascimento = dateTime.Ticks;
            aluno.Telefone = Telefone.Text;

            App.Client.UpdateAlunoAsync(aluno);
            App.Client.UpdateAlunoCompleted += ClientOnUpdateAlunoCompleted;
        }

        private void ClientOnUpdateAlunoCompleted(object sender, AsyncCompletedEventArgs asyncCompletedEventArgs)
        {
            App.Client.UpdateAlunoCompleted -= ClientOnUpdateAlunoCompleted;
            Busy.IsBusy = false;
            MessageBox.Show("Aluno Editado");
            Busy.DataContext = new Random().Next();
        }
    }
}